// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify infoTree
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package app.displayTree.analyze.texture {
    import app.debug.APPLog;
    import app.utils.SWFInfoTreeImageUtils;

    import copyengine.infoTree.meta.CESTextureMeta;
    import copyengine.utils.UUIDFactory;

    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.utils.Dictionary;

    public final class DisplayTreeTextureGenerate {
        private var mAllExportTextureBitmapDic:Dictionary;
        private var mAllUnMergeTextureWrapVector:Vector.<McTextureWrap>;

        public function DisplayTreeTextureGenerate() {
            mAllExportTextureBitmapDic = new Dictionary();
            mAllUnMergeTextureWrapVector = new Vector.<McTextureWrap>();
        }


        public function getTargetTextureMeta(_target:DisplayObject):CESTextureMeta {
            var texture:CESTextureMeta = new CESTextureMeta();
            var wrap:McTextureWrap = new McTextureWrap();
            wrap.targetMc = _target;
            wrap.textureMeta = texture;
            mAllUnMergeTextureWrapVector.push(wrap);
            return texture;
        }

        public function exportAllTextureMetaVector():Vector.<CESTextureMeta> {
            var result:Vector.<CESTextureMeta> = new Vector.<CESTextureMeta>();
            for each (var wrap:McTextureWrap in mAllUnMergeTextureWrapVector) {
                result.push(wrap.textureMeta);
            }
            return result;
        }

        public function exportTextureBitmapDic():Dictionary {
            for each (var wrap:McTextureWrap in mAllUnMergeTextureWrapVector) {
                if (wrap.targetMc.width == 0 || wrap.targetMc.height == 0) {
                    APPLog.err("Can't export targetMc : " + wrap.targetMc.name + " width or height is 0, can't export...... please check the error!");
                    continue;
                }
                var newImg:BitmapData = SWFInfoTreeImageUtils.cacheDisplayObjectToBitmapWithNormalMatrix(wrap.targetMc);
                for (var key:String in mAllExportTextureBitmapDic) {
                    var oldImg:BitmapData = mAllExportTextureBitmapDic[key];
                    if (oldImg.compare(newImg) == 0) {
                        wrap.textureMeta.textureKey = key;
                        break;
                    }
                }

                //====如果没有则产生新的Texture
                if (wrap.textureMeta.textureKey == null) {
                    var newKey:String = UUIDFactory.instance.generateUUID();
                    mAllExportTextureBitmapDic[newKey] = newImg;
                    wrap.textureMeta.textureKey = newKey;
                }
            }

            return mAllExportTextureBitmapDic;
        }


    }
}
